//
//  MomentImagesCellAdapter.m
//  WeChatMoments
//
//  Created by Jiang on 2019/8/26.
//  Copyright © 2019 SilverFruity. All rights reserved.
//

#import "MomentImagesCellAdapter.h"
#import "ImageCollectionLayoutCalculator.h"


@interface MomentImagesCellAdapter() <UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation MomentImagesCellAdapter
- (void)setCell:(MomentImagesCell *)cell{
    cell.imageCollectionView.delegate = self;
    cell.imageCollectionView.dataSource = self;
    _cell = cell;
}
- (void)setImages:(NSArray<ImagesItem *> *)images{
    // 计算collectionView的宽高和itemSize
    ImageCollectionLayoutCalculator *calculator = [ImageCollectionLayoutCalculator calculatorWithItemCount:images.count
                                                                                                cellMargin:10
                                                                                    singleItemViewMaxWidth:UIScreen.mainScreen.bounds.size.width - 100 - self.cell.cellectionViewLeftConstraint.constant
                                                                                      fourItemViewMaxWidth:UIScreen.mainScreen.bounds.size.width - 100 - self.cell.cellectionViewLeftConstraint.constant
                                                                                      nineItemViewMaxWidth:UIScreen.mainScreen.bounds.size.width - 44 - self.cell.cellectionViewLeftConstraint.constant];
    self.cell.collectionViewHeihgtConstraint.constant = [calculator viewHeight];
    self.cell.collectionViewWidthConstraint.constant = [calculator viewHeight];
    self.cell.flowLayout.itemSize = [calculator itemSize];
    self.cell.flowLayout.minimumInteritemSpacing = calculator.cellMargin;
    self.cell.flowLayout.minimumLineSpacing = calculator.cellMargin;
    _images = images;
    [self.cell.imageCollectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.images.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MomentImageCollectionViewCell" forIndexPath:indexPath];
    UIImageView *imageView = cell.contentView.subviews.firstObject;
    [imageView sp_setImageWithURL:self.images[indexPath.row].url placholder:UIImageWithColor([UIColor lightGrayColor])];
    return cell;
}


@end

